package ManageTools;

import org.redisson.Redisson;
import org.redisson.api.RAtomicDouble;
import org.redisson.api.RAtomicLong;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

/**
 * @author zhang kun
 * @Classname RedissonManager
 * @Description TODO
 * @Date 2021/8/3 21:37
 */
public class RedissonManager {
    private static final String RAtomicName = "genId_";
    private static Config config = new Config();
    private static Redisson redisson = null;

    public static void init(){
        try{
            config.useClusterServers() //这是用的集群server
                    .setScanInterval(2000) //设置集群状态扫描时间
                    .setMasterConnectionPoolSize(10000) //设置连接数
                    .setSlaveConnectionPoolSize(10000)
                    .addNodeAddress("redis://127.0.0.1:6379","redis://127.0.0.1:6380");

            redisson = (Redisson) Redisson.create(config);
            RAtomicLong atomicLong = redisson.getAtomicLong(RAtomicName);
            atomicLong.set(1L);


        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static Redisson getRedisson(){
        return redisson;
    }

    /**
     * 获取原子ID
     * @return
     */
    public static Long nextID(){
        RAtomicLong atomicLong = getRedisson().getAtomicLong(RAtomicName);
        atomicLong.incrementAndGet();
        return atomicLong.get();
    }
}
